home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / winsock / wschesb1.zip / SRC / PIECE.C < prev    next >
C/C++ Source or Header  |  1994-03-15  |  5KB  |  173 lines

  1. /*
  2.   C source for Winsock Chess
  3.   
  4.   Revision 1994-03-15
  5.   Modified by Donald Munro for use as a 2 player chess game over a 
  6.   WINSOCK layer on a TCP (or other WinSock supporting) network.
  7.   Source code and make files for MS Visual C/C++ V1.00/1.50.
  8.   February/March 1994
  9.   All GNU copyright and distribution conditions as described below and in the
  10.   file COPYING also apply to WinSock Chess.
  11.   This module is adapted from GNU Chess.
  12.   
  13.   C source for GNU CHESS
  14.  
  15.   Revision: 1990-09-30
  16.  
  17.   Modified by Daryl Baker for use in MS WINDOWS environment
  18.  
  19.   Based on Ideas and code segments of Charles Petzold from artices in
  20.   MicroSoft Systems Journal.
  21.  
  22.   This file is part of CHESS.
  23.  
  24.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  25.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  26.   the consequences of using it or for whether it serves any particular
  27.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  28.   General Public License for full details.
  29.  
  30.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  31.   only under the conditions described in the CHESS General Public License.
  32.   A copy of this license is supposed to have been given to you along with
  33.   CHESS so you can know your rights and responsibilities.  It should be in a
  34.   file named COPYING.  Among other things, the copyright notice and this
  35.   notice must be preserved on all copies.
  36. */
  37.  
  38. #define NOATOM 
  39. #define NOCLIPBOARD
  40. #define NOCREATESTRUCT
  41. #define NOFONT
  42. #define NOREGION
  43. #define NOSOUND
  44. #define NOWH
  45. #define NOWINOFFSETS
  46. #define NOCOMM
  47. #define NOKANJI
  48.  
  49. #include <windows.h>
  50. #include <stdio.h>
  51.  
  52. #include "chess.h"
  53. #include "defs.h"
  54.  
  55. extern struct PIECEBITMAP pieces[6];
  56.  
  57. #define PIECE_XAXIS 32
  58. #define PIECE_YAXIS 32
  59.  
  60. static void QuerySqCenter ( short x, short y, POINT *pptl);
  61. static short ConvertCoordToIndex ( short x, short y);
  62. static void PieceOriginFromCenter ( POINT *pptl);
  63. static void QuerySqPieceOrigin ( short x, short y, POINT *pptl);
  64. static void DrawOnePiece ( HDC hDC, short x, short y, struct PIECEBITMAP *piece, DWORD color);
  65. static void ShowPiece ( HDC hDC, POINT *pptl, struct PIECEBITMAP *Piece_bitmap,
  66.                   DWORD Color );
  67.  
  68. static void QuerySqCenter ( short x, short y, POINT *pptl)
  69. {
  70.    POINT aptl[4];
  71.  
  72.    QuerySqCoords ( x, y, aptl );
  73.  
  74.    pptl->x = (aptl[0].x + aptl[1].x + aptl[2].x + aptl[3].x)/4;
  75.    pptl->y = (aptl[0].y + aptl[2].y)/2;
  76. }
  77.  
  78.  
  79. static void PieceOriginFromCenter ( POINT *pptl)
  80. {
  81.    pptl->x -= PIECE_XAXIS / 2;
  82.    pptl->y -= PIECE_YAXIS / 2;
  83. }
  84.  
  85. static void QuerySqPieceOrigin ( short x, short y, POINT *pptl)
  86. {
  87.       QuerySqCenter ( x, y, pptl);
  88.       PieceOriginFromCenter (pptl);
  89. }
  90.  
  91.  
  92. /*
  93.    Draw a piece in the specificed point
  94.  
  95.    Piece_bitmap is a structure with the handles for the mask,
  96.    outline and piece.
  97.  
  98. */
  99.  
  100. static void ShowPiece ( HDC hDC, POINT *pptl, struct PIECEBITMAP *Piece_bitmap,
  101.                   DWORD Color )
  102. {
  103.    HDC hMemDC;
  104.    HBRUSH hBrush, hOldBrush;
  105.    HPEN hOldPen;
  106.  
  107.  
  108.    hOldBrush = SelectObject ( hDC, GetStockObject (BLACK_BRUSH) );
  109.    hOldPen = SelectObject ( hDC, GetStockObject ( BLACK_PEN) );
  110.    
  111.    hMemDC = CreateCompatibleDC ( hDC);
  112.  
  113.    /* Write the mask to clear the space */
  114.    SelectObject ( hMemDC, Piece_bitmap->mask);
  115.    BitBlt ( hDC, pptl->x, pptl->y, PIECE_XAXIS, PIECE_YAXIS, hMemDC, 0, 0,SRCAND);
  116.  
  117.    /* Write out the piece with an OR */
  118.  
  119.    hBrush = CreateSolidBrush ( Color );
  120.    SelectObject ( hDC, hBrush );
  121.  
  122.    SelectObject ( hMemDC, Piece_bitmap->piece);
  123.    BitBlt ( hDC, pptl->x, pptl->y, PIECE_XAXIS, PIECE_YAXIS, hMemDC, 0, 0,0xB80746L);
  124.  
  125.    /* The draw the outline */
  126.  
  127.    SelectObject ( hDC, GetStockObject ( BLACK_BRUSH ) );
  128.    SelectObject ( hMemDC, Piece_bitmap->outline);
  129.    BitBlt ( hDC, pptl->x, pptl->y, PIECE_XAXIS, PIECE_YAXIS, hMemDC, 0, 0, 0xB80746L);
  130.  
  131.  
  132.    SelectObject ( hDC, hOldBrush );
  133.    SelectObject ( hDC, hOldPen );
  134.    DeleteObject ( hBrush);
  135.  
  136.    if ( DeleteDC(hMemDC)==0) MessageBeep(0);
  137. }
  138.  
  139. static short ConvertCoordToIndex ( short x, short y)
  140. {
  141.    return (y*8 + x );
  142. }
  143.  
  144. static void DrawOnePiece ( HDC hDC, short x, short y, struct PIECEBITMAP *piece, DWORD color)
  145. {
  146.    POINT origin;
  147.  
  148.    QuerySqPieceOrigin ( x, y, &origin );
  149.    ShowPiece ( hDC, &origin, piece, color);
  150.  
  151. }
  152.  
  153.  
  154.  
  155. void DrawAllPieces ( HDC hDC,  int reverse, short *pbrd, short *color,
  156.                      DWORD clrblack, DWORD clrwhite )
  157. {
  158.    short x,y;
  159.    short i;
  160.  
  161.    for ( y=0; y<8; y++) {
  162.       for (x=0; x<8; x++) {
  163.          i = ConvertCoordToIndex (x, y);
  164.          if ( *(color+i) != NETURAL ) {
  165.             if (reverse == 0)
  166.                DrawOnePiece ( hDC, x, y, pieces+*(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  167.             else
  168.                DrawOnePiece ( hDC, 7-x, 7-y, pieces+*(pbrd+i), (*(color+i)==BLACK) ? clrblack : clrwhite );
  169.          }
  170.       }
  171.    }
  172. }
  173.